package com.chance.cc.crawler.development.bootstrap.douyin;

import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequest;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import com.chance.cc.crawler.core.selector.Json;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import static com.chance.cc.crawler.core.script.crawler.CrawlerScript.$_type;

/**
 * @author bx
 * @date 2021/6/28 0028 10:15
 */
public class DYDevicesCheck {

    public static void main(String[] args) throws IOException {

        List<String> devices = IOUtils.readLines(new FileInputStream("D:\\chance\\soft\\douyin\\devices\\unavailable_devices.txt"), "UTF-8");
        HttpConfig httpConfig = HttpConfig.me("test");
//        httpConfig.setProxy(proxy);
//        httpConfig.setUseProxy(true);
//
        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());

        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl("http://192.168.1.215:9599/crawler/oauth/api/v1/douyin/userOauthInfos");
        HttpPage download = downloader.download(httpRequest, httpConfig);

        String results = download.getRawText();
        Json rawText = new Json(results);
        String status = rawText.jsonPath($_type + ".status").get();
        List<String> contents = rawText.jsonPath($_type + ".content").all();
        Map<String, String> devicesMap = new HashMap<>();
        if ("0".equals(status) && contents != null && contents.size() > 0) {

            List<Map> userOauthInfos = new Json(contents.get(0)).toList(Map.class);
            for (Map userOauthInfo : userOauthInfos) {
                String oauthInfo = String.valueOf(userOauthInfo.get("oauthInfo"));
                String createTime = String.valueOf(userOauthInfo.get("createTime"));
                Json oauthInfoJson = new Json(oauthInfo);
                String category = oauthInfoJson.jsonPath($_type + ".category").get();

                if ("device_comment".equals(category)) {
                    String device_id = oauthInfoJson.jsonPath($_type + ".device_id").get();
                    devicesMap.put(device_id, createTime);
                }
            }
        }
        Map<String, String> resultsMap = new HashMap<>();
        for (String device : devices) {
            if (devicesMap.containsKey(device)) {
                resultsMap.put(device, DateFormatUtils.format(Long.valueOf(devicesMap.get(device)), "yyyyMMdd"));
                System.out.println("device: " + device + ",create time: " + devicesMap.get(device));
            }
        }
        List<String> resultsList = new ArrayList<>();
        for (Map.Entry<String, String> entry : resultsMap.entrySet()) {
            resultsList.add(entry.getKey() + "\t" + entry.getValue());
        }
        IOUtils.writeLines(resultsList,"\r\n",new FileOutputStream("D:\\chance\\soft\\douyin\\devices\\unavailable_devices_time.txt"),"utf-8");
    }
}
